package com.opal.qualityControl.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.opal.qualityControl.entity.SalesorgInBound;
import com.opal.qualityControl.entity.SalesorgOutBound;
import com.opal.qualityControl.form.InDateForm;
import com.opal.qualityControl.mapper.oracle.NotSavedStockListMapper;
import com.opal.qualityControl.service.NotSavedStockListService;
import com.opal.utils.ExcelDownloadUtil;
import com.opal.utils.PageVOUtils;
import com.opal.vo.PageVO;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class NotSavedStockListServiceImpl implements NotSavedStockListService {

    @Autowired
    private NotSavedStockListMapper notSavedStockListMapper;

    @Override
    public PageVO getNotSavedStockList( int page, int rows,InDateForm inDateForm,int selectCodeH) {
        inDateForm.setFirstIndex((page - 1) * rows);
        inDateForm.setLastIndex(page * rows);
        switch (selectCodeH)
        {
            case 1:
                //产成品入库单
                int count =notSavedStockListMapper.getFinishedProductInCount(inDateForm);
                List<SalesorgInBound> list = notSavedStockListMapper.getFinishedProductIn(inDateForm);
                return PageVOUtils.success(count,list,200,"");
            case 2:
                //销售出库单
                int count1= notSavedStockListMapper.getSalesorgOutBoundCount(inDateForm);
                List<SalesorgOutBound> list1 = notSavedStockListMapper.getSalesorgOutBound(inDateForm);
                return PageVOUtils.success(count1,list1,200,"");
            case 3:
                //调拨出库单
                int count2 = notSavedStockListMapper.getTransoutCount(inDateForm);
                List<SalesorgOutBound> list2 = notSavedStockListMapper.getTransout(inDateForm);
                return PageVOUtils.success(count2,list2,200,"");
        }
        return null;
    }

    @Override
    public void notSavedStockOutExcel(int selectCodeH, InDateForm inDateForm, HttpServletRequest request, HttpServletResponse response) {
        String name="";
        String date =inDateForm.getNotSavedStartTime().substring(0, 7);
        Class tepy = SalesorgOutBound.class ;
        List<?> list = new ArrayList<>();
        switch (selectCodeH)
        {
            case 1:
                name="销售出库单";
                tepy=SalesorgInBound.class;
                list= notSavedStockListMapper.getFinishedProductIn(inDateForm);
                break;
            case 2:
                name="调拨出库单";
                tepy= SalesorgOutBound.class;
                list = notSavedStockListMapper.getSalesorgOutBound(inDateForm);
                break;
            case 3:
                name="产成品入库单";
                tepy=SalesorgOutBound.class;
                list = notSavedStockListMapper.getTransout(inDateForm);
                break;

        }
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(name, date),tepy, list);
        try {
            ExcelDownloadUtil.ExcelUtil(response, workbook, name+" "+date+".xls",request);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
